package com.example.demo.controller;

import com.example.demo.entity.ArticleInfoVO;
import com.example.demo.service.ArticleInfoService;
import com.example.util.BaseController;
import com.example.util.exception.BigThreeException;
import com.example.util.exception.BigThreeExceptionEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzr on 2020/12/04.
 */
@Controller
@CrossOrigin
@RequestMapping("bigthree")
public class ArticleInfoController extends BaseController {

    @Resource
    private ArticleInfoService articleInfoService;

    /**
     * 查询单个文章
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/articleByName")
    @ResponseBody
    public Object getArticleByName(HttpServletRequest request, Model model) {

        List<ArticleInfoVO> listResult = null;
        String article_name = request.getParameter("title");
        listResult = this.articleInfoService.getArticleByName(article_name);
        return listResult;

    }

    /**
     * 文章列表
     *
     * @param author    文章作者
     * @param label     文章一级标签
     * @param subLabel  文章二级标签
     * @param beginTime 开始时间
     * @param endTime   结束时间
     */
    @RequestMapping(value = "/articleList")
    @ResponseBody
    public Object articleList(@RequestParam(required = false) String author,
                              @RequestParam(required = false) String label,
                              @RequestParam(required = false) String subLabel,
                              @RequestParam(required = false) String beginTime,
                              @RequestParam(required = false) String endTime) {
        List<Map<String, Object>> listResult;
        HashMap<String, Object> hashMap = new HashMap<>();
        listResult = articleInfoService.articleList(label, subLabel, author, beginTime, endTime);
        if (listResult == null || listResult.size() == 0) {
            throw new BigThreeException(BigThreeExceptionEnum.LIST_IS_NULL);
        }
        hashMap.put("data", listResult);
        return hashMap;
    }

    /**
     * 新增数据同步
     */
    @RequestMapping(value = "/addArticle")
    @ResponseBody
    public Object addArticle(ArticleInfoVO articleInfoVO) {
        boolean flag = articleInfoService.addArticle(articleInfoVO);
        if (flag) {
            return SUCCESS_TIP;
        } else {
            return ERROR;
        }
    }

    /**
     * 删除数据同步
     */
    @RequestMapping(value = "/deleteArticle")
    @ResponseBody
    public Object deleteById(@RequestParam Integer id) {
        boolean flag = articleInfoService.deleteArticleById(id);
        if (flag) {
            return SUCCESS_TIP;
        } else {
            return ERROR;
        }
    }

    /**
     * 修改文章信息
     */
    @RequestMapping(value = "/updateArticle")
    @ResponseBody
    public Object updateById(ArticleInfoVO articleInfoVO) {
        boolean flag = articleInfoService.updateArticle(articleInfoVO);
        if (flag) {
            return SUCCESS_TIP;
        } else {
            return ERROR;
        }
    }
}
